from django.db import models

# Create your models here.

class Users(models.Model):
	username = models.CharField(max_length=50)
	password = models.CharField(max_length=80)
	email =  models.CharField(max_length=50)
	phone =  models.CharField(max_length=11)
	address = models.CharField(max_length=50,null =True)
	age =  models.IntegerField(null=True)
	sex = models.CharField(max_length=2)
	status = models.CharField(max_length=30,null =True)
	pic = models.CharField(max_length=100,default='/static/public/pics/1.jpg')
	addtime = models.DateTimeField(auto_now_add=True)


# 商品分类
class Types(models.Model):
    name = models.CharField(max_length=20)
    pid = models.IntegerField()
    path = models.CharField(max_length=20)


# 商品信息
class Goods(models.Model):
    typeid = models.ForeignKey(to="Types", to_field="id")
    title = models.CharField(max_length=255)
    price = models.DecimalField(max_digits=8, decimal_places=2)
    storage = models.IntegerField()
    pic = models.CharField(max_length=50)
    info = models.TextField()
    status = models.IntegerField()
    addtime = models.DateField(auto_now_add=True)

# 订单表
class Order(models.Model):
    uid = models.ForeignKey('Users',to_field='id')
    addname = models.CharField(max_length=50)
    address = models.CharField(max_length=255)
    addphone = models.CharField(max_length=11)
    addcode = models.CharField(max_length=10)
    totalprice = models.FloatField()
    totalnum = models.IntegerField()
    status = models.IntegerField()
    addtime = models.DateTimeField(auto_now_add=True)

# 订单详情
class OrderInfo(models.Model):
    # 一个用户可以有多个订单
    orderid = models.ForeignKey('Order',to_field="id")
    # 一件商品可以包含在多个订单里
    gid = models.ForeignKey('Goods',to_field="id")
    num = models.IntegerField()
    price = models.FloatField()
    
    # 外键,在中大型项目, I/O 阻塞,性能,外键关系维护,锁表
    # 用不用外键,不是要考虑数据库,而是设计架构的事

